package service.impl;

import utils.MyBatisUtils;
import dao.CommunicationRecordMapper;
import entity.CommunicationRecord;
import service.CommunicationService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 沟通记录服务实现类
 */
public class CommunicationServiceImpl implements CommunicationService {

    @Override
    public void createRecord(CommunicationRecord record) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            CommunicationRecordMapper mapper = sqlSession.getMapper(CommunicationRecordMapper.class);
            mapper.insert(record);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("创建沟通记录失败: " + e.getMessage());
        }
    }

    @Override
    public CommunicationRecord getRecordById(Long id) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            CommunicationRecordMapper mapper = sqlSession.getMapper(CommunicationRecordMapper.class);
            return mapper.selectById(id);
        }
    }

    @Override
    public List<CommunicationRecord> getRecordsByApplicationId(Long applicationId) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            CommunicationRecordMapper mapper = sqlSession.getMapper(CommunicationRecordMapper.class);
            return mapper.selectByApplicationId(applicationId);
        }
    }

    @Override
    public void updateRecord(CommunicationRecord record) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            CommunicationRecordMapper mapper = sqlSession.getMapper(CommunicationRecordMapper.class);
            mapper.updateById(record);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("更新沟通记录失败: " + e.getMessage());
        }
    }

    @Override
    public void deleteRecord(Long id) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            CommunicationRecordMapper mapper = sqlSession.getMapper(CommunicationRecordMapper.class);
            mapper.deleteById(id);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("删除沟通记录失败: " + e.getMessage());
        }
    }
}
